//
//  BCStuffChooseViewController.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/13.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCStuffChooseViewController.h"
#import "WKPageView.h"
#import "BCSearchResulfViewController.h"
#import "BCOrderViewController.h"
#import "BCStuffInfoViewController.h"

@interface BCStuffChooseViewController ()<WKPageViewDataSource,WKPageViewDelegate,shopCarDelegate, clickDelegate>
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, weak)   WKPageView *pageView;
@property (nonatomic, strong)NSArray *dataArr;
/** 搜索框绿色渐变背景视图 */
@property (nonatomic, strong) UIView *greenView;
/** 搜索view */
@property (nonatomic, strong) UIView *searchView;
@property (nonatomic, strong) UIButton *backButton;
@end


@implementation BCStuffChooseViewController
- (instancetype)initWithIndex:(NSInteger )index
{
    self = [super init];
    if (self) {
        self.ClickIndex = index;
        [self getValue];
        [self getView];
    }
    return self;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initTopView];
    [self p_initSearchView];
}

-(void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;

    [BCShopCarManager sharedInstance].delegate = self;
    [[BCShopCarManager sharedInstance] layOutWithLogo];
    [BCShopCarManager sharedInstance].butTitle = @"下订单";

    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[BCShopCarManager sharedInstance] hideShopWindow];
}

- (void)logoTo
{
    BCOrderViewController *secondViewController = [[BCOrderViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];

}

- (void)getValue
{
    self.items = [NSArray arrayWithObjects:@"禽蛋肉品",@"蔬菜水果",@"水产冻品",@"调料其它",@"米面粮油",@"熟食面点",@"酒水饮料",@"厨房用品", nil];
        NSArray *arr0 = @[@"全部",
                          @"猪肉",
                          @"牛肉",
                          @"羊肉",
                          @"禽类",
                          @"猪肉冻",
                          @"禽类冻",
                          @"牛羊肉冻",
                          @"蛋类",
                          @"活禽",
                          @"调理肉类"];
        NSArray *arr1 = @[@"全部",
                          @"叶菜类",
                          @"茄果类",
                          @"根茎类",
                          @"豆类",
                          @"特菜",
                          @"菌类",
                          @"葱姜蒜",
                          @"水果"];
        NSArray *arr2 = @[@"全部",
                          @"水产类",
                          @"水发类",
                          @"冷冻水产",
                          @"火锅丸类"];
        NSArray *arr3 = @[@"全部",
                          @"酱油醋",
                          @"腌菜罐头",
                          @"香辛料",
                          @"调味油",
                          @"调味酱",
                          @"糖、盐",
                          @"南北干货",
                          @"调味料",
                          @"其他"];
        NSArray *arr4 = @[@"全部",
                          @"大米",
                          @"面粉面条",
                          @"杂粮",
                          @"食用油"];
        NSArray *arr6 = @[@"全部",
                          @"乳品",
                          @"白酒红酒",
                          @"啤酒",
                          @"饮料",
                          @"饮用水"];
        NSArray *arr5 = @[@"全部",
                          @"主食",
                          @"熟食",
                          @"冷冻面点",
                          @"豆制品"];
        NSArray *arr7 = @[@"全部",
                          @"清洁用品",
                          @"厨房用具",
                          @"一次性消耗品"];
        self.dataArr = [NSArray arrayWithObjects:arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, nil];
        
    
}

- (void)getView
{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    WKPageView *pageView = [[WKPageView alloc] initWithFrame:CGRectMake(0, 65.f, self.view.bounds.size.width, self.view.bounds.size.height-65.f)];
    pageView.dataSource = self;
    pageView.delegate = self;
    pageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:pageView];
    self.pageView = pageView;
    pageView.index = self.ClickIndex;

}

- (void)goTo
{
    BCOrderViewController *secondViewController = [[BCOrderViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];

}

- (void)goSecond{
    NSLog(@"click Second");
}

- (void)goThirdWithUUID:(NSString *)uuid
{
    BCStuffInfoViewController *stuffVC = [[BCStuffInfoViewController alloc] init];
    stuffVC.stuff_uuid = uuid;
    [self.navigationController pushViewController:stuffVC animated:YES];
}

/**
 *  初始化顶部视图view
 */
- (void)p_initTopView {
    self.greenView.frame = CGRectMake(0, 0, self.view.frame.size.width , 65);
    self.greenView.backgroundColor = BangcaiGreen;
    self.greenView.alpha = 0.95f;
    [self.view addSubview:self.greenView];
}

/**
 *  初始化搜索框view
 */
- (void)p_initSearchView {
    
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackArrow"]];;
    backImageView.frame = CGRectMake(17.5f, 30.f, 10.f, 20.f);
    [self.greenView addSubview:backImageView];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame = CGRectMake(0, 0, 40, 40);
    self.backButton.center = backImageView.center;
    [self.backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.greenView addSubview:self.backButton];
    
    UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(45.f, 0, self.greenView.frame.size.width-45.f-16.f, 29.f)];
    searchBar.center = CGPointMake(searchBar.center.x, backImageView.center.y);
    searchBar.backgroundColor = [UIColor colorWithRed:0/255.f green:162.f/255.f blue:72.f/255.f alpha:1];
    searchBar.layer.cornerRadius = 8.f;
    [self.greenView addSubview:searchBar];
    
    UIImageView *searchIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索"]];
    searchIconView.frame = CGRectMake(8.f, 8.f, 15.f, 15.f);
    [searchBar addSubview:searchIconView];
    
    UILabel *tipLable = [[UILabel alloc] initWithFrame:CGRectMake(searchIconView.frame.origin.x+searchIconView.frame.size.width+2.f, 0, searchBar.frame.size.width-(searchIconView.frame.origin.x*2)-searchIconView.frame.size.width, searchBar.frame.size.height)];
    tipLable.text = @"去搜索";
    tipLable.backgroundColor = [UIColor clearColor];
    tipLable.font = [UIFont boldSystemFontOfSize:15.f];
    tipLable.textAlignment = NSTextAlignmentLeft;
    tipLable.textColor = [UIColor whiteColor];
    [searchBar addSubview:tipLable];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(searchBar.frame.origin.x, 0, searchBar.frame.size.width, self.greenView.frame.size.height);
    searchButton.backgroundColor = [UIColor clearColor];
    [searchButton addTarget:self action:@selector(p_startSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.greenView addSubview:searchButton];
}

#pragma mark - page view datasource
// Menu的标题用NSArray封装，内为NSString
- (NSArray *)menuItemsForMenuViewInPageView:(WKPageView *)pageView{
    return self.items;
}

- (WKPageCell *)pageView:(WKPageView *)pageView cellForIndex:(NSInteger)index{
    static NSString *identifier = @"pageCell";
    WKPageCell *cell = [pageView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[WKPageCell alloc] initWithIdentifier:identifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    cell.secondArr = self.dataArr[index];
    cell.thirdIndex = index;
    cell.delegate = self;
    cell.reddotAnimationBlock = ^(CAAnimation *anim) {
        if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {
            
            CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            shakeAnimation.duration = 0.25f;
            shakeAnimation.fromValue = [NSNumber numberWithFloat:0.9];
            shakeAnimation.toValue = [NSNumber numberWithFloat:1];
            shakeAnimation.autoreverses = YES;
            [[BCShopCarManager sharedInstance].logoImg.layer addAnimation:shakeAnimation forKey:nil];
        }
    };
    return cell;
}
// 若不实现，默认红黑，为保证字体颜色渐变，请尽量选用RGBA来创建UIColor
- (UIColor *)titleColorOfMenuItemInPageView:(WKPageView *)pageView withState:(WKMenuItemTitleColorState)state{
    if (state == WKMenuItemTitleSizeStateNormal) {
        return [UIColor colorWithRed:0.2  green:0.2  blue:0.2 alpha:1];
    }else if (state == WKMenuItemTitleSizeStateSelected)
    {
        return [UIColor colorWithRed:0.154  green:0.775  blue:0.409 alpha:1];
    }else
    {
        return [UIColor redColor];
    }
}

// 若不实现,默认为30
- (CGFloat)pageView:(WKPageView *)pageView heightForMenuView:(WKMenuView *)menuView{
    return 50;
}
// 若不实现，默认为15/18
- (CGFloat)titleSizeOfMenuItemInPageView:(WKPageView *)pageView withState:(WKMenuItemTitleSizeState)state{
    switch (state) {
        case WKMenuItemTitleSizeStateNormal:
            return 14;
            break;
        case WKMenuItemTitleSizeStateSelected:
            return 14;
        default:
            break;
    }
}
// 若不实现，默认为灰色
- (UIColor *)backgroundColorOfMenuViewInPageView:(WKPageView *)pageView{
    return [UIColor whiteColor];
}
// MenuView内部各个item的宽度，若标题过长可自行设置，默认为60
- (CGFloat)pageView:(WKPageView *)pageView widthForMenuItemAtIndex:(NSInteger)index{
    return 80;
}

#pragma mark - action
/**
 *  点击搜索框事件
 */
- (void)p_startSearch {
    BCSearchResulfViewController *searchResultViewController = [[BCSearchResulfViewController alloc] init];
    [self.navigationController pushViewController:searchResultViewController animated:YES];
}

-(void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Lazy
- (UIView *)greenView {
    if (_greenView == nil) {
        _greenView = [[UIView alloc] init];
    }
    return _greenView;
}


@end
